package itbour.onetouchshow.activity.version1_6;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.version1_2.CutVideoActivity;
import itbour.onetouchshow.activity.version1_4.ImageClipActivity;
import itbour.onetouchshow.activity.video.make.MakeActivity;
import itbour.onetouchshow.activity.video.make.MakePresenter;
import itbour.onetouchshow.bean.ClipVideoBean;
import itbour.onetouchshow.bean.canvas.PosBean;
import itbour.onetouchshow.bean.inching.noppt.InchingCoreRootNoPPTV10Bean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.listener.SoftKeyBoardListener;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by Administrator on 2018/5/16.
 */

public class MakeActivityV1_6 extends MakeActivity {


    @BindView(R.id.it_hint_view)
    protected LinearLayout hintView;
    VideoEditAdapterV1_6 videoEditAdapter;
    LinearLayoutManager linearLayoutManager;

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        mPresenter = new MakePresenter(this, this);
        super.afterCreate(savedInstanceState);
        llRoot.setVisibility(View.GONE);
    }

    @Override
    protected void initKeyBroadListener() {
        // super.initKeyBroadListener();
        SoftKeyBoardListener.setListener(MakeActivityV1_6.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                if (llBootmView != null) {
                    llBootmView.setVisibility(View.GONE);
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
                    layoutParams.weight = height;
                    hintView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void keyBoardHide(int height) {
                L_.i("键盘隐藏 高度" + height);
                // videoEditAdapter.getEditTextView().clearFocus();
                if (llBootmView != null) {
                    llBootmView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (llBootmView != null) {
                                llBootmView.setVisibility(View.VISIBLE);
                                hintView.setVisibility(View.GONE);
                            }
                        }
                    }, 30);
                }
            }
        });


    }

    protected void initRvEditVideo() {
        linearLayoutManager = new LinearLayoutManager(MakeActivityV1_6.this, LinearLayoutManager.VERTICAL, false);
        videoEditAdapter = new VideoEditAdapterV1_6(MakeActivityV1_6.this, wantData);
        videoEditAdapter.setOnPictureIvClickListener(new VideoEditAdapterV1_6.OnPictureIvClickListener() {
            @Override
            public void getPictureIv() {
                //去裁界面
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, AppConst.TOPHOTO);
            }

            @Override
            public void addBoundView(InchingCoreRootNoPPTV10Bean data) {
            }

            @Override
            public void removeBoundView() {
            }

            @Override
            public void getCurrentPic(int position, String url) {
            }
        });
        //videoEditAdapter.add
        //idRvData.addOnScrollListener(new MyRecyclerViewScrollListener());
        /**
         * 从底部网上布局
         */
        // linearLayoutManager.setStackFromEnd(true);
        idRvData.setLayoutManager(linearLayoutManager);
        idRvData.setAdapter(videoEditAdapter);
        // idRvData.scrollBy(0,0);
        //linearLayoutManager.scrollToPositionWithOffset(0, 0);
    }


    /**
     * 从不同的界面返回 执行不同的操作
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // super.onActivityResult(requestCode, resultCode, data);
        if (videoEditAdapter == null) {
            return;
        }
        //选择视频回调
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
                    String videoUrl = null;
                    for (LocalMedia media : selectList) {
                        Log.i("图片-----》", media.getPath());
                        videoUrl = media.getPath();
                    }
                    String[] strings = UIUtils.videoWHDA(videoUrl);
                    String width = strings[0];//宽
                    String height = strings[1];//高
                    String angle = strings[2];
                    Intent intent = new Intent(MakeActivityV1_6.this, CutVideoActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(AppConst.VIDEOURL, videoUrl);
                    bundle.putString(AppConst.VIDEOW, width);
                    bundle.putString(AppConst.VIDEOH, height);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, 8000);
                    break;

            }
        }

        if (data != null) {
            if (requestCode == AppConst.TOPHOTO) {
                //图片裁剪
                if (videoEditAdapter.getNowImgRootValue().getPos().getServiceH() == -1) {
                    videoEditAdapter.getNowImgRootValue().getPos().setServiceH(videoEditAdapter.getNowImgRootValue().getPos().getH());
                }
                if (videoEditAdapter.getNowImgRootValue().getPos().getServiceW() == -1) {
                    videoEditAdapter.getNowImgRootValue().getPos().setServiceW(videoEditAdapter.getNowImgRootValue().getPos().getW());
                }
                int serviceWidth = (int) videoEditAdapter.getNowImgRootValue().getPos().getServiceW();
                int serviceHeight = (int) videoEditAdapter.getNowImgRootValue().getPos().getServiceH();
                List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                String imageUrl = null;
                for (LocalMedia media : localMedia) {
                    // Log.e("图片-----》", media.getPath());
                    imageUrl = media.getPath();
                }
                // String end = imageUrl.substring(imageUrl.lastIndexOf(".") + 1, imageUrl.length()).toLowerCase();
                Intent intent = ImageClipActivity.getLaunchIntent(MakeActivityV1_6.this, imageUrl, serviceWidth, serviceHeight);
                //Intent intent = CommonUtils.createToPhotoIntent(data, MakeActivityV1_6.this, serviceWidth, serviceHeight);
                if (intent == null) {
                    T_.showCustomToast("选择的文件不是图片", true);
                    return;
                }
                startActivityForResult(intent, AppConst.CHANGE_IMG_RESULT);
            } else if (requestCode == AppConst.CHANGE_IMG_RESULT) {
                String pathName = data.getStringExtra(AppConst.LOCAL_PHOTO_PATH_NAME);
                L_.i("CHANGE_IMG_RESULT===" + pathName);
                //设置选择图片真实宽高

                videoEditAdapter.getNowImgRootValue().getPos().setW(data.getIntExtra(AppConst.PHOTO_BITMAP_W, -1));
                videoEditAdapter.getNowImgRootValue().getPos().setH(data.getIntExtra(AppConst.PHOTO_BITMAP_H, -1));
                videoEditAdapter.getNowImgRootValue().setValue(pathName);
                ImageView nowClickImg = videoEditAdapter.getNowClickImg();
                Glide.with(MakeActivityV1_6.this).applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.place_icon)
                        .error(R.drawable.place_icon)).load(pathName).thumbnail(0.1f).into(nowClickImg);

                videoEditAdapter.getNowImgMask().bringToFront();
                videoEditAdapter.videoNotifyItemChanged(false);

            } else if (requestCode == AppConst.REFRESH) {
                //String videoUrl = data.getStringExtra("videoUrl");
                // String imageUrl = data.getStringExtra("imageUrl");
                ClipVideoBean clipVideoBean = (ClipVideoBean) data.getSerializableExtra("videoBean");
                // T_.showCustomToast(clipVideoBean.toString());
                videoEditAdapter.getRootVideoValueV10Bean().setUrl(clipVideoBean.getVideoUrl());
                videoEditAdapter.getRootVideoValueV10Bean().setValue(clipVideoBean.getVideoThumb());
                videoEditAdapter.getRootVideoValueV10Bean().setDuration(Double.valueOf(clipVideoBean.getD()));
                PosBean pos = videoEditAdapter.getRootVideoValueV10Bean().getPos();
                pos.setH(Float.valueOf(clipVideoBean.getH()));
                pos.setW(Float.valueOf(clipVideoBean.getW()));
                pos.setAn(Integer.valueOf(clipVideoBean.getR()));
                L_.e("requestCode" + JSON.toJSONString(videoEditAdapter.getRootVideoValueV10Bean()));
                ImageView nowClickImg = videoEditAdapter.getNowClickImg();
                //.thumbnail(0.1f)
                Glide.with(MakeActivityV1_6.this).applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.place_icon)
                        .error(R.drawable.place_icon)).load(clipVideoBean.getVideoThumb()).into(nowClickImg);
                videoEditAdapter.videoNotifyItemChanged(true);

            }
        }
    }


}
